package com.java.code.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.java.code.utils.Stringutils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class JavaHttpRequest {
    public static String httpPost(String url, List<NameValuePair> nameValuePairs) throws Exception {
        //模拟创建一个浏览器用户
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        httppost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
        httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        if (nameValuePairs != null && nameValuePairs.size() > 0) {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }
        //使用httpclient进行连接
        CloseableHttpResponse response = client.execute(httppost);
        //查询成功
        String content = "";
        if (response.getStatusLine().getStatusCode() == 200) {
            content = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(content);
        } else {
            System.out.println("返回失败: " + response.getStatusLine().getStatusCode());
        }
        //每次请求等待2秒
        Thread.sleep(1000);
        return content;
    }

    public static String httpGet(String url, List<NameValuePair> nameValuePairList) throws Exception {
        CloseableHttpClient client = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(url);
        uriBuilder.addParameters(nameValuePairList);
        HttpGet httpGet = new HttpGet(uriBuilder.build());

        httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
        httpGet.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8"));
        CloseableHttpResponse response = client.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString(entity, "UTF-8");
            return content;
        }
        return "";
    }

    public static <T> T httpGet(String url, List<NameValuePair> nameValuePairList, Class<T> clazz) throws Exception {
        String content = httpGet(url, nameValuePairList);
        if (Stringutils.isNotEmpty(content)) {
            ObjectMapper mapper = new ObjectMapper();
            T t = mapper.readValue(content, clazz);
            return t;
        }
        return null;
    }

}
